home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ML_BME1.ZIP / _LIB_ / ALLOC.PAS next >
Encoding:
Pascal/Delphi Source File  |  1996-11-14  |  4.4 KB  |  209 lines

  1. Unit Alloc;
  2. {
  3.   Memory allocation and deallocation routines (C type), without using Heap
  4.   by Maple Leaf, 1996
  5.   -------------------
  6.   386 registers used.
  7.   Last update: 2nd May 1996
  8. }
  9. Interface
  10.  
  11. { --------<<< Conventional memory >>>---------- }
  12.  
  13. Const
  14.   UseUMB : Boolean = True;  { If TRUE, the malloc routine will try to allocate
  15.                               the memory block into the Upper Memory (if possible) }
  16.  
  17. function malloc(size:LongInt):pointer;
  18. { Allocates a memory block, returns a pointer to it. Offset is always 0. (paragraph alligned) }
  19. function calloc(size:LongInt):pointer;
  20. { Like MALLOC, but it does initialize the block with 0 }
  21. function halloc(size:LongInt):word;
  22. { Like MALLOC, returns the segment nr. of the allocated block }
  23. function realloc(var MemPtr:Pointer; size:LongInt):LongInt;
  24. { Reallocates a memory block, returns the new maximal possible size (could be with up to 16 bytes greater than SIZE) }
  25. { Warning: if returned value is less than SIZE, then the reallocation IS NOT DONE, you must
  26.   do it by yourself using this value as a parameter in realloc function. }
  27.  
  28. function free(var MemPtr:pointer):boolean;
  29. { Deallocates a memory block, returns True if successful }
  30. function hfree(var MemSeg:word):boolean;
  31. { Like FREE, uses block's seg, returns True if successful }
  32.  
  33. function mavail:LongInt;
  34. { Returns the amount of bytes in the greatest possible block }
  35. function umblink(LinkStatus:boolean):boolean;
  36. { Returns the current status of UMB linkage and set it to LinkStatus }
  37.  
  38. Implementation
  39.  
  40. function _malloc(size:LongInt):pointer;assembler;
  41. asm
  42.      db 66h; mov ax,word ptr Size
  43.      test ax,0Fh
  44.      pushf
  45.      db 66h; shr ax,4
  46.      popf
  47.      jz @1
  48.      db 66h; inc ax
  49. @1:  db 66h; mov bx,ax   { ebx:=((size div 16) + 1) paragraphs }
  50.      mov ah,48h
  51.      clc
  52.      int 21h    {Allocate memory}
  53.      jc @Error
  54.      xor dx,dx
  55.      xchg ax,dx
  56.      jmp @Exit
  57. @Error:
  58.      xor dx,dx
  59.      xor ax,ax
  60. @Exit:
  61. end;
  62.  
  63. function _realloc(var MemPtr:Pointer;size:LongInt):LongInt;assembler;
  64. asm
  65.      les di,MemPtr
  66.      les di,es:[di]
  67.      db 66h; mov ax,word ptr Size
  68.      test ax,0Fh
  69.      pushf
  70.      db 66h; shr ax,4
  71.      popf
  72.      jz @1
  73.      db 66h; inc ax
  74. @1:  db 66h; mov bx,ax  { ebx:=(NewSize div 16)+1) paragraphs }
  75.      clc
  76.      mov ah,4ah
  77.      int 21h
  78.      db 66h; rol bx,16
  79.      xor bx,bx
  80.      db 66h; rol bx,16+4
  81.      mov ax,bx
  82.      db 66h; shr bx,16
  83.      mov dx,bx
  84. @Exit:
  85. end;
  86.  
  87. function _free(var MemPtr:pointer):boolean;assembler;
  88. asm
  89.      les di,MemPtr
  90.      les di,es:[di]
  91.      mov ax,es
  92.      or ax,ax
  93.      jz @Exit
  94.      mov ah,49h
  95.      clc
  96.      int 21h
  97.      mov ax,0
  98.      jc @Exit
  99.      les di,MemPtr
  100.      mov word ptr es:[di],0
  101.      mov word ptr es:[di+2],0
  102.      mov ax,1
  103. @Exit:
  104. end;
  105.  
  106. function _mavail:LongInt;assembler;
  107. asm
  108.      mov ax,4800h
  109.      mov bx,0FFFFh
  110.      clc
  111.      int 21h
  112.      clc
  113.      xor dx,dx
  114.      mov ax,bx
  115.      mov cx,16
  116.      mul cx
  117. end;
  118.  
  119. function umblink(LinkStatus:boolean):boolean;assembler;
  120. asm
  121.   mov ax,5802h
  122.   int 21h
  123.   mov ah,0
  124.   jc @getout
  125.   mov ah,al
  126.   push ax
  127.   mov ax,5803h
  128.   xor bh,bh
  129.   mov bl,LinkStatus
  130.   int 21h
  131.   pop ax
  132. @getout:
  133.   mov al,ah
  134. end;
  135.  
  136. function malloc;
  137. var SaveLink:Boolean;
  138. begin
  139.   if UseUMB then SaveLink:=umblink(true);
  140.   malloc:=_malloc(size);
  141.   if UseUMB then umblink(SaveLink);
  142. end;
  143.  
  144. function free;
  145. var SaveLink:Boolean;
  146. begin
  147.   if UseUMB then SaveLink:=umblink(true);
  148.   free:=_free(MemPtr);
  149.   if UseUMB then umblink(SaveLink);
  150. end;
  151.  
  152. function realloc;
  153. var SaveLink:Boolean;
  154. begin
  155.   if UseUMB then SaveLink:=umblink(true);
  156.   realloc:=_realloc(MemPtr,Size);
  157.   if UseUMB then umblink(SaveLink);
  158. end;
  159.  
  160. function mavail;
  161. var SaveLink:Boolean;
  162. begin
  163.   if UseUMB then SaveLink:=umblink(true);
  164.   mavail:=_mavail;
  165.   if UseUMB then umblink(SaveLink);
  166. end;
  167.  
  168. function calloc;
  169. var p:pointer;
  170. begin
  171.   p:=malloc(size);
  172.   asm
  173.       cmp word ptr p[2],0 {nil ?}
  174.       je @getout
  175.       db 66h; mov cx,word ptr size
  176.       les di,p
  177.       mov ax,1000h
  178.   @1: mov es:[di],al
  179.       dec ah
  180.       jg @2
  181.       mov ah,10h
  182.       mov di,es
  183.       inc di
  184.       mov es,di
  185.       xor di,di
  186.   @2: db 66h; dec cx  {ecx}
  187.       db 66h; jnz @1
  188.   @getout:
  189.   end;
  190.   calloc:=p;
  191. end;
  192.  
  193. function halloc;
  194. var p:pointer;
  195. begin
  196.   p:=malloc(size);
  197.   halloc:=word(longint(p) shr 16);
  198. end;
  199.  
  200. function hfree;
  201. var p:pointer;
  202. begin
  203.   p:=ptr(MemSeg,0);
  204.   MemSeg:=0;
  205.   hfree:=free(p);
  206. end;
  207.  
  208. begin
  209. end.